﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace Common
{
    public class LocationHelp
    {
        public static string GetLocaton(string ipCode)
        {
            string api = string.Format("http://www.youdao.com/smartresult-xml/search.s?type=ip&q={0}", ipCode);
            string strReturn = string.Empty;
            string strHTML = string.Empty;
            try
            {
                //获得服务器上的请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api);
                //请求结果
                HttpWebResponse result = (HttpWebResponse)request.GetResponse();
                //获取服务器的响应流
                Stream receviceStream = result.GetResponseStream();
                StreamReader readerOfStream = new StreamReader(receviceStream, Encoding.GetEncoding("GB2312"));
                strHTML = readerOfStream.ReadToEnd();

                CYQ.Data.Xml.XmlHelper xmlHelp = new CYQ.Data.Xml.XmlHelper(false);
                //从字符串中读取XML内容
                xmlHelp.LoadXml(strHTML);

                System.Xml.XmlNodeList byNodeList = xmlHelp.GetList("product");
                System.Xml.XmlNode parentNode = null;
                if (byNodeList != null && byNodeList.Count > 0)
                {
                    foreach (System.Xml.XmlNode item in byNodeList)
                    {
                        parentNode = item;
                    }
                }

                string res = xmlHelp.Get("location", null, null, parentNode).FirstChild.Value;

                string[] List = res.Split(new char[] { ' ' });
                strReturn = List[0];
                readerOfStream.Close();
                receviceStream.Close();
                result.Close();
                return strReturn;
            }
            catch (Exception e)
            {
                ErrorHelper.RecordError(e.ToString() + "Html:" + strHTML + "返回值：" + strReturn);
                return e.ToString() + "hnrich";
            }
        }

        /// <summary>
        /// 通过URL获取数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static DataSet GetDataSetByUrl(string ipCode)
        {
            string url = string.Format("http://www.youdao.com/smartresult-xml/search.s?type=ip&q={0}", ipCode);
            DataSet ds = new DataSet();
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(url);
                stream = new StringReader(xmlDoc.InnerXml);
                reader = new XmlTextReader(stream);
                ds.ReadXml(reader);
                return ds;
            }
            catch
            {
                return null;
            }
            finally
            {
                if (reader != null) reader.Close();
            }
        }


    }


}
